//
//  SearchInstituViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/10/28.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "SearchInstituViewController.h"
#import "OrganationBottomCell.h"
#import "InstitutionsDetailContainViewController.h"
#import "InstituteModel.h"
@interface SearchInstituViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@end

@implementation SearchInstituViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chooseDataAndReload:) name:@"reloadSearchData" object:nil];
    // Do any additional setup after loading the view.
}
-(void)chooseDataAndReload:(NSNotification *)infor{
//    NSLog(@"你想刷新吗%@",infor.userInfo[@"dataArray"]);
    self.instituArray = infor.userInfo[@"dataArray"];
     [self createCollectionUI];
    NSLog(@"你想刷新吗%@",self.instituArray);
//    [self.collectionView reloadData];
}
#pragma mark -创建collectionView
-(void)createCollectionUI{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.sectionHeadersPinToVisibleBounds = YES;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - (64 + 40)) collectionViewLayout:self.flowLayout];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeaderone"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"LLKcollectionFooter"];
    UINib *nib1 = [UINib nibWithNibName:@"ListenStoryHeaderCell" bundle:nil];
    [self.collectionView registerNib:nib1 forCellWithReuseIdentifier:@"ListenStoryHeaderCell"];
    [self.collectionView registerClass:[OrganationBottomCell class] forCellWithReuseIdentifier:@"OrganationBottomCell"];
    [self.collectionView reloadData];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.instituArray.count;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    OrganationBottomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OrganationBottomCell" forIndexPath:indexPath];
    for (UIView *subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    cell.model = self.instituArray[indexPath.row];
    cell.detailbtn.tag = 1000 + indexPath.row;
    return cell;
    
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(WIDTH, scaleTopAndBottom(100));
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return scaleTopAndBottom(1);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return scaleLeftAndRight(1);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    InstituteModel *model = self.instituArray[indexPath.row];
    InstitutionsDetailContainViewController *vc = [[InstitutionsDetailContainViewController alloc] init];
    vc.instituId = model.Id;
    vc.nameStr = model.name;
    vc.addressStr = [NSString stringWithFormat:@"%@%@%@%@",model.province,model.city,model.area,model.addresss];
    self.navBlock(vc);
    
}
-(NSMutableArray *)resultsArray{
    if (_instituArray == nil) {
        _instituArray = [[NSMutableArray alloc] init];
    }
    return _instituArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
